import { getData } from '@vimi/utils-api'
import { SERVICE_NAME } from '@/gov/plugins/constant'

const state = {
  dams: [],
}

const getters = {
  dams: state => state.dams,
}

const actions = {
  async getDam({ commit, rootGetters }, force = false) {
    if (!force && state.dams.length) {
      return Promise.resolve(state.dams)
    }

    const res = await getData(SERVICE_NAME, 'dam_info', {
      address_code: rootGetters.user.queryAddressCode,
      fields: ['dam_name', 'dam_info_id', 'address_code'],
    })
    const data = (res && res.data) || []
    commit('dams', data)
    return data
  },
}

const mutations = {
  dams: (state, payload) => (state.dams = payload),
}

export default {
  state,
  getters,
  actions,
  mutations,
}
